home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / fstab / mtab.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  5KB  |  180 lines

  1. #include <string.h>
  2. #include "fstab.h"
  3. #include "../paths.h"
  4. #include "../netconf/netconf.h"
  5. #include "fstab.m"
  6.  
  7. static FSTAB_HELP_FILE help_mtab ("mtab");
  8. static CONFIG_FILE f_mtab (ETC_MTAB,help_mtab,CONFIGF_NONE);
  9.  
  10.  
  11. PUBLIC MTAB::MTAB ()
  12. {
  13.     FILE *fin = f_mtab.fopen("r");
  14.     if (fin != NULL){
  15.         char buf[1000];
  16.         while (fgets_cont(buf,sizeof(buf)-1,fin) != -1){
  17.             add (new FSTAB_ENTRY(buf));
  18.         }
  19.         fclose (fin);
  20.     }
  21.     rstmodified();
  22. }
  23.  
  24. /*
  25.     Locate one FSTAB_ENTRY using the mount point as the key
  26. */
  27. PUBLIC FSTAB_ENTRY *FSTAB_GEN::locate_mpoint(const char *str)
  28. {
  29.     FSTAB_ENTRY *ret = NULL;
  30.     int n = getnb();
  31.     for (int i=0; i<n; i++){
  32.         FSTAB_ENTRY *e = getitem(i);
  33.         if (strcmp(e->getmpoint(),str)==0){
  34.             ret = e;
  35.             break;
  36.         }
  37.     }
  38.     return ret;
  39. }
  40.  
  41. static int fstab_domount(FSTAB_ENTRY *efs)
  42. {
  43.     char buf[1000];
  44.     int len = sprintf (buf,"-t %s ",efs->getfs());
  45.     char opt[200];
  46.     efs->format_opt (0,opt);
  47.     if (opt[0] != '\0'){
  48.         len += sprintf (buf+len," -o %s",opt);
  49.     }
  50.     sprintf (buf+len," %s %s",efs->getsource(),efs->getmpoint());
  51.     return netconf_system_if ("mount",buf);
  52. }
  53.  
  54. static int fstab_checkmount(
  55.     FSTAB_ENTRY *efs,    // Entry in fstab
  56.     FSTAB_ENTRY *emt)    // Corresponding entry found in /etc/mtab
  57. {
  58.     int ret = 0;
  59.     if (emt == NULL){
  60.         /* #Specification: fstab / consistency / mount auto
  61.             Only mount spec with the option "auto" (mount at boot time)
  62.             are managed during the integrity check. If they are already
  63.             mount, then a check is done to insure they are corectly mounted.
  64.             If they are not mounted, nothing is done.
  65.         */
  66.         if (efs->is_auto()){
  67.             // Not mount, let's do it
  68.             ret = fstab_domount (efs);
  69.         }
  70.     }else{
  71.         char old_opt[200];
  72.         emt->format_opt (0,old_opt);
  73.         char new_opt[200];
  74.         efs->format_opt (0,new_opt);
  75.         if (strcmp(efs->getsource(),emt->getsource())!=0){
  76.             /* #Specification: fstab / consistency / changing mount source
  77.                 When we detect that the source of a mount has changed
  78.                 (for a given mount point), we must ask the operator for
  79.                 a permission to unmount and mount again. A failure
  80.                 to answer we be taken as a "NO, don't do it" after a timeout
  81.                 which may be active for this session.
  82.  
  83.                 Anyway, this will often fail because something is currently
  84.                 accessing the mounted resource.
  85.             */
  86.             char buf[1000];
  87.             sprintf (buf,MSG_U(I_FIXMOUNT
  88.                 ,"The directory %s was originally mounted\n"
  89.                  "on %s. It should now be mounted\n"
  90.                  "on %s.\n"
  91.                  "\n"
  92.                  "Should I unmount/mount to fix it ?")
  93.                 ,efs->getmpoint(),emt->getsource(),efs->getsource());
  94.             if (simul_ison()
  95.                 || xconf_yesno(MSG_U(Q_FIXMOUNT,"Fix target of a mount")
  96.                     ,buf,help_mtab)==MENU_YES){
  97.                 ret = netconf_system_if("umount",emt->getmpoint());
  98.                 if (ret == 0) ret = fstab_domount (efs);
  99.             }
  100.         }else if(strcmp(old_opt,new_opt)!=0){
  101.             /* #Specification: fstab / consistency / changing mount option
  102.                 When we detect that the options of a mount have changed
  103.                 (for a given mount point), we must ask the operator for
  104.                 a permission to remount. A failure
  105.                 to answer we be taken as a "NO, don't do it" after a timeout
  106.                 which may be active for this session.
  107.  
  108.                 Anyway, this will often fail because something is currently
  109.                 accessing the mounted resource.
  110.             */
  111.             char buf[1000];
  112.             sprintf (buf,MSG_U(I_REMOUNT
  113.                 ,"The directory %s was originally mounted\n"
  114.                  "with option  %s.\n"
  115.                  "It should now be mounted\n"
  116.                  "with options %s.\n"
  117.                  "\n"
  118.                  "Should I remount to fix it ?")
  119.                 ,efs->getmpoint(),old_opt,new_opt);
  120.             if (simul_ison()
  121.                 || xconf_yesno(MSG_U(Q_REMOUNT,"activate new mount options")
  122.                     ,buf,help_mtab)==MENU_YES){
  123.                 int len = sprintf (buf,new_opt[0] == '\0'
  124.                     ? "-o remount" : "-o remount,%s"
  125.                     ,new_opt);
  126.                 sprintf (buf+len," %s",efs->getmpoint());
  127.                 ret = netconf_system_if("mount",buf);
  128.             }
  129.         }
  130.     }
  131.     return ret;
  132. }
  133.  
  134. /*
  135.     Check that all mountable file system are currently
  136.     mounted.
  137. */
  138. int fstab_checkmount(
  139.     int local_fs)    // Local or network mount
  140. {
  141.     FSTAB fstab;
  142.     MTAB mtab;
  143.     /* #Specification: fstab / check fstab and mtab
  144.         Linuxconf can check if all file system specified
  145.         in /etc/fstab are currently mounted. Furthermore
  146.         it can even check that something else is mounted
  147.         in place of what is specified in /etc/fstab. In this
  148.         situation it will umount and remount.
  149.  
  150.         This situation generally occur when we edit
  151.         /etc/fstab, changing the source (server) of a volume.
  152.  
  153.         However, linuxconf will not unmount something that is not
  154.         specify in /etc/fstab. The mount may have been done manually
  155.         or by an automounter.
  156.     */
  157.     int nbfs = fstab.getnb();
  158.     int ret = 0;
  159.     for (int i=0; i<nbfs; i++){
  160.         FSTAB_ENTRY *efs = fstab.getitem(i);
  161.         if (efs->is_valid()){
  162.             FSTAB_ENTRY_TYPE type = efs->gettype();
  163.             const char *mpoint = efs->getmpoint();
  164.             FSTAB_ENTRY *emt = mtab.locate_mpoint(mpoint);
  165.             if (local_fs){
  166.                 if (type == FSTAB_ENTRY_LOCAL
  167.                     || type == FSTAB_ENTRY_PROC){
  168.                     ret |= fstab_checkmount (efs,emt);
  169.                 }
  170.             }else{
  171.                 if (type == FSTAB_ENTRY_NFS){
  172.                     ret |= fstab_checkmount (efs,emt);
  173.                 }
  174.             }
  175.         }
  176.     }
  177.     return ret;
  178. }            
  179.  
  180.